package com.dy.lock.screen.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.CustomerAttentionComponent;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.CustomerAttentionInfo;
import com.dy.lock.screen.pojo.CustomerBlacklistInfo;
import com.dy.lock.screen.pojo.CustomerMarkInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.CustomerAttentionService;
import com.dy.lock.screen.service.CustomerBlacklistService;
import com.dy.lock.screen.service.CustomerMarkRelationService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.vo.CustomerAttentionVo;
import com.google.gson.Gson;

@Component("customerAttentionComponent")
public class CustomerAttentionComponentImpl implements
		CustomerAttentionComponent {

	private static final Logger log = Logger
			.getLogger(CustomerAttentionComponentImpl.class);
	private Gson gson;

	@Resource(name = "customerAttentionService")
	private CustomerAttentionService customerAttentionService;
	@Resource(name = "customerMarkRelationService")
	private CustomerMarkRelationService customerMarkRelationService;
	@Resource(name = "customerBlacklistService")
	private CustomerBlacklistService customerBlacklistService;

	public CustomerAttentionComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean attentionOrCancel(String body, Head head) {
		CustomerAttentionVo vo = gson.fromJson(body, CustomerAttentionVo.class);
		if (vo != null) {
			CustomerAttentionInfo attInfo = new CustomerAttentionInfo();
			attInfo.setAttentionId(vo.getAttentionId());
			attInfo.setBeConcernedId(vo.getBeConcernedId());
			int result = -1;
			if (vo.isAttentionOrNot()) {
				CustomerAttentionInfo att = this.customerAttentionService
						.queryByAidAndBeid(attInfo);
				CustomerBlacklistInfo blacklistInfo = new CustomerBlacklistInfo();
				blacklistInfo.setBlackCustomer(vo.getAttentionId());
				blacklistInfo.setBeBlackedCustomer(vo.getBeConcernedId());
				CustomerBlacklistInfo bl = customerBlacklistService
						.queryByBlackIdAndBeBlackId(blacklistInfo);
				blacklistInfo.setBlackCustomer(vo.getBeConcernedId());
				blacklistInfo.setBeBlackedCustomer(vo.getAttentionId());
				CustomerBlacklistInfo bl2 = customerBlacklistService
						.queryByBlackIdAndBeBlackId(blacklistInfo);

				if (bl != null || bl2 != null) {
					log.info("requestId:" + head.getRequestId()
							+ "当前用户已把该用户加入黑名单，不能再关注该用户。");
					throw new ServerDisponseException(
							TransmissionCode.DO_NOT_ATTENTION_USER_BECAUSE_BLACK_USER
									.getCode(),
							TransmissionCode.DO_NOT_ATTENTION_USER_BECAUSE_BLACK_USER
									.getMessage());
				}
				if (att == null) {
					result = this.customerAttentionService.save(attInfo);
				} else {
					log.info("requestId:" + head.getRequestId()
							+ "当前用户已经关注过该用户。");
					throw new ServerDisponseException(
							TransmissionCode.CUSTOMER_ATTENTIONED.getCode(),
							TransmissionCode.CUSTOMER_ATTENTIONED.getMessage());
				}
			} else {
				result = this.customerAttentionService.delete(attInfo);
			}
			if (result > 0) {
				return true;
			} else {
				throw new ServerDisponseException(
						TransmissionCode.OPERATOR_EXCEPTION.getCode(),
						TransmissionCode.OPERATOR_EXCEPTION.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",用户关注操作时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}

	}

	@Override
	public List<CustomerAttentionInfo> attentionList(String body, Head head) {
		CustomerAttentionVo vo = gson.fromJson(body, CustomerAttentionVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> para = new HashMap<String, Object>();
			para.put("pageStart", pageStart);
			para.put("pageEnd", pageEnd);
			para.put("attentionId", vo.getAttentionId());
			List<CustomerAttentionInfo> list = this.customerAttentionService
					.queryPage(para);
			if (list != null && !list.isEmpty()) {
				for (CustomerAttentionInfo info : list) {
					if (StringUtils.isBlank(info.getUserName())) {
						info.setUserName(info.getUserId());
					}
					if (StringUtils.isNoneBlank(info.getHeaderPath())) {
						info.setHeaderPath(CommonUtil
								.getUrlPrefixApi("photo.url.prefix.path.api")
								+ info.getHeaderPath());
					}
					CustomerAttentionInfo att = new CustomerAttentionInfo();
					att.setAttentionId(vo.getUserId());
					att.setBeConcernedId(info.getUserId());
					CustomerAttentionInfo atten = this.customerAttentionService
							.queryByAidAndBeid(att);
					if (atten != null) {
						info.setAttentioned(true);
					}
					List<CustomerMarkInfo> markInfos = this.customerMarkRelationService
							.queryListByCid(info.getUserId());
					info.setMarkInfoList(markInfos);
				}
				return list;
			} else {
				return new ArrayList<CustomerAttentionInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId()
					+ ",获取用户关注列表操作时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<CustomerAttentionInfo> fansList(String body, Head head) {
		CustomerAttentionVo vo = gson.fromJson(body, CustomerAttentionVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> para = new HashMap<String, Object>();
			para.put("pageStart", pageStart);
			para.put("pageEnd", pageEnd);
			para.put("beConcernedId", vo.getBeConcernedId());
			List<CustomerAttentionInfo> list = this.customerAttentionService
					.queryPage(para);
			if (list != null && !list.isEmpty()) {
				for (CustomerAttentionInfo info : list) {
					if (StringUtils.isBlank(info.getUserName())) {
						info.setUserName(info.getUserId());
					}
					if (StringUtils.isNoneBlank(info.getHeaderPath())) {
						info.setHeaderPath(CommonUtil
								.getUrlPrefixApi("photo.url.prefix.path.api")
								+ info.getHeaderPath());
					}
					CustomerAttentionInfo att = new CustomerAttentionInfo();
					att.setAttentionId(vo.getUserId());
					att.setBeConcernedId(info.getUserId());
					CustomerAttentionInfo atten = this.customerAttentionService
							.queryByAidAndBeid(att);
					if (atten != null) {
						info.setAttentioned(true);
					}
					List<CustomerMarkInfo> markInfos = this.customerMarkRelationService
							.queryListByCid(info.getUserId());
					info.setMarkInfoList(markInfos);
				}
				return list;
			} else {
				return new ArrayList<CustomerAttentionInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId()
					+ ",获取用户粉丝列表操作时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
